//
//  SearchTableView.swift
//  Wmsmobile
//
//  Created by wangshuning on 16/8/22.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//


import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


class SearchTableView: UITableView, UITableViewDelegate, UITableViewDataSource ,DetailListCellDisplayViewProtcol {
    
    //MARK: - property
    var realHeightDelegate: DetailListCellProtcol?
    
    var heightDelegate: DetailListCellProtcol?{
        set{
            realHeightDelegate = newValue
        }
        get{
            return realHeightDelegate
        }
    }
    var anyInfo:AnyObject?{
        didSet{
            dataArray = anyInfo as? [[String : String]]
        }
    }
    var dataArray:[[String : String]]?{
        didSet{
            self.reloadData()
            if self.delegate != nil{
                self.heightDelegate?.setRealHeight(44 * CGFloat((dataArray?.count)!))
            }
        }
    }
    
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        self.register(SearchDetailTableViewCell.self, forCellReuseIdentifier: "BinCel")
        self.delegate = self
        self.dataSource = self
        self.isScrollEnabled = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - tableViewDelegate
    
    //    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
    //        let row = Int(point.y / 44) + 1//自己判断点击了那个cell
    //        if row != lastRow && self.dataArray != nil {
    //            lastRow = row
    //            let info = self.dataArray![row]
    //            let vc = binDetailTableViewController(info: info)
    //            self.parentController()?.navigationController?.pushViewController(vc, animated: true)
    //        }
    //
    //        return super.hitTest(point, withEvent: event)
    //    }
    //MARK: - dataSource
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray?.count ?? 1
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BinCel") as! SearchDetailTableViewCell
        if self.dataArray?.count > 0{
            let info = self.dataArray![(indexPath as NSIndexPath).row]
            cell.info = info
        }
        return cell
    }
    
}
